package com.collection.service.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.collection.dao.WebsiteStrategyDao;
import com.collection.entity.UrlParam;
import com.collection.entity.WebsiteStrategy;
import com.collection.service.WebsiteStrategyService;

@Service
public class WebsiteStrategyServiceImpl implements WebsiteStrategyService {

	@Autowired
	private WebsiteStrategyDao websiteStrategyDao;
	
	/*
	 * (non-Javadoc)
	 * @see com.collection.service.WebsiteStrategyService#createWebsiteStrategy(com.collection.entity.WebsiteStrategy)
	 */
	@Override
	public void createWebsiteStrategy(WebsiteStrategy websiteStrategy) {
		if(websiteStrategy != null) {
			websiteStrategyDao.save(websiteStrategy);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.collection.service.WebsiteStrategyService#getWebsiteStrategyByTaskId(java.lang.Integer)
	 */
	public WebsiteStrategy getWebsiteStrategyByTaskId(Integer taskId) {
		if(taskId != null) {
			List<WebsiteStrategy> websiteStrategies = websiteStrategyDao.queryByHQL("from WebsiteStrategy ws where ws.collectionTask.id=?", new Object[]{taskId});
			if(websiteStrategies != null && websiteStrategies.size()>0) {
				return websiteStrategies.get(0);
			}
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see com.collection.service.WebsiteStrategyService#deleteWebsiteStrategy(java.lang.Integer)
	 */
	@Override
	public void deleteWebsiteStrategy(Integer id) {
		if(id != null) {
			WebsiteStrategy websiteStrategy = getWebsiteStrategyById(id);
			if(websiteStrategy != null) {
				deleteWebsiteStrategy(websiteStrategy);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.collection.service.WebsiteStrategyService#deleteWebsiteStrategy(com.collection.entity.WebsiteStrategy)
	 */
	@Override
	public void deleteWebsiteStrategy(WebsiteStrategy websiteStrategy) {
		if(websiteStrategy != null) {
			websiteStrategyDao.delete(websiteStrategy);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.collection.service.WebsiteStrategyService#getWebsiteStrategyById(java.lang.Integer)
	 */
	@Override
	public WebsiteStrategy getWebsiteStrategyById(Integer id) {
		if(id != null) {
			return websiteStrategyDao.get(WebsiteStrategy.class, id);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see com.collection.service.WebsiteStrategyService#modifyWebsiteStrategy(com.collection.entity.WebsiteStrategy)
	 */
	@Override
	public void modifyWebsiteStrategy(WebsiteStrategy websiteStrategy) {
		if(websiteStrategy != null) {
			websiteStrategyDao.update(websiteStrategy);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see
	 * com.collection.service.CollectionTaskService#createUrl(java.util.List,
	 * java.lang.String, int, java.util.List)
	 */
	public void createUrl(List<UrlParam> urlParams, String url, int i,
			List<String> urls) {
		List<String> tempUrls = new ArrayList<String>();
		int size = urlParams.size();
		UrlParam urlParam = urlParams.get(i);
		Integer start = urlParam.getStart();
		Integer end = urlParam.getEnd();
		Integer step = urlParam.getStep();
		do {
			String tempUrl = StringUtils.replace(url, "{*}", start.toString(),
					1);
			tempUrls.add(tempUrl);
			start += step;
		} while (start <= end);

		if (size > 1 && i < size - 1) {
			i++;
			for (String tempUrl : tempUrls) {
				createUrl(urlParams, tempUrl, i, urls);
			}
		} else if (i == size - 1) {
			for (String tempUrl : tempUrls) {
				urls.add(tempUrl);
			}
			return;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.collection.service.CollectionTaskService#getHtmlContent(java.lang
	 * .String, java.lang.String)
	 */
	public String getHtmlContent(String path, String charCode) {
		URL url = null;
		BufferedReader br = null;
		String str = "";
		StringBuffer sb = new StringBuffer();
		try {
			url = new URL(path);
			// System.out.println(url.getAuthority() + ",\n" +
			// url.getDefaultPort() + ",\n" + url.getFile() + ",\n" +
			// url.getHost() + ",\n"
			// +url.getPath() + ",\n" + url.getPort() + ",\n" +url.getQuery() +
			// ",\n"+url.getRef() + ",\n"+url.getUserInfo() +
			// ",\n"+url.getContent());

			br = new BufferedReader(new InputStreamReader(url.openStream(),
					charCode));

			while ((str = br.readLine()) != null) {
				sb.append(str.trim());
			}

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null) {
					br.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

}
